package org.group6.utils;

import de.dailab.graphworld.ontology.Node;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 *
 * @author Kseniya
 */
public abstract class AgentUtils {

    public static final double DELTA = 10E1;

    public static List<Node> optimizePath(List<Node> path) {
        List<Node> pathCopied = new ArrayList<>(path);

        for (int i = 0; i < pathCopied.size(); i++) {
            Node cycleStart = pathCopied.get(i);
            int lastIndex = findLastNodeIndexByID(pathCopied, cycleStart);
            Node lastFound = pathCopied.get(lastIndex);
            if (lastFound != cycleStart) {
                // only one of both nodes must be in updated path
                // include cycleStart
                List<Node> first = pathCopied.subList(0, i + 1);
                // exclude lastFound
                List<Node> last = pathCopied.subList(lastIndex + 1, pathCopied.size());
                pathCopied = new ArrayList<>(first.size() + last.size());
                pathCopied.addAll(first);
                pathCopied.addAll(last);
            }
        }
        return pathCopied;
    }

    public static String printAllUniqueNodes(List<Node> nodes) {
        Set<Integer> ids = new HashSet<>();
        for (Node node : nodes) {
            ids.add(Integer.valueOf(node.getID()));
        }
        List<Integer> idsOrdered = new ArrayList<>(ids);
        Collections.sort(idsOrdered);
        return idsOrdered.toString();
    }

    private static int findLastNodeIndexByID(Collection<Node> nodes, Node toFind) {
        int counter = 0, index = 0;
        for (Node node : nodes) {
            if (node.getID() == toFind.getID()) {
                index = counter;
            }
            counter++;
        }
        return index;
    }

    public static double roundPheromone(double pheromone) {
        return Math.round(pheromone * DELTA) / DELTA;
    }
}
